package com.fsds.crem.web.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jxls.transformer.XLSTransformer;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fsds.crem.core.feature.orm.mybatis.Page;
import com.fsds.crem.core.util.DateUtils;
import com.fsds.crem.web.entity.NodeInfo;
import com.fsds.crem.web.entity.SaleDailyReport;
import com.fsds.crem.web.service.SaleDailyReportService;

@Controller
@RequestMapping("/report/day/")
public class ReportDayController {

	private Logger logger = Logger.getLogger(ReportDayController.class);
	
	@Resource
	private SaleDailyReportService saleDailyReportService;
	
	/**
	 * 报表查询，及获取报表数据，分页显示
	 * 
	 * @param request
	 * @param saleDailyReport
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request,SaleDailyReport saleDailyReport,String pageNo) {

		int pageNoInt = 1;
		
		if (pageNo != null && !"".equals(pageNo) ) {
			
				// 页码是数字的验证在前台处理
				pageNoInt = Integer.parseInt(pageNo); 
		}
		
		Page<SaleDailyReport> page = new Page<SaleDailyReport>(pageNoInt);
		
		List<SaleDailyReport> reports = saleDailyReportService.findByPage(saleDailyReport, page).getResult();

		request.setAttribute("reports", reports);
		request.setAttribute("page", page);
		
		List<NodeInfo> nodeInfoList = saleDailyReportService.findNodeList();
		request.setAttribute("nodeInfoList", nodeInfoList);

		return "report/reportdaylist";
	}
	
	/**
	 * 导出excel
	 * 
	 * @param request
	 * @param response
	 * @param saleDailyReport
	 */
	@RequestMapping(value="/print")
	public void print(HttpServletRequest request,HttpServletResponse response,SaleDailyReport saleDailyReport) {
		
		InputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			
			// 项目根目录
			String rootPath = request.getSession().getServletContext().getRealPath("/");
			// 模板路径
			String templatePath =  rootPath + "/export/template/saleDailyReport_export.xls";
			// 生成的临时文件路径
			String filePath = rootPath + "/export/" + System.currentTimeMillis() + ".xls";
			
			// 导出数据
			Map<String,Object> beanParams = new HashMap<String,Object>();  
			List<SaleDailyReport> reports = saleDailyReportService.findAllBySearch(saleDailyReport);
			beanParams.put("list", reports);
			beanParams.put("printDate", DateUtils.getDate("yyyy/MM/dd"));
			beanParams.put("printTime", DateUtils.getDate("HH:mm:ss"));
			beanParams.put("printUserName", "admin");
			
			//创建XLSTransformer对象  
			XLSTransformer transformer = new XLSTransformer(); 
			//生成Excel文件  
			transformer.transformXLS(templatePath, beanParams, filePath);
			
			response.setContentType("application/octet-stream;charset=UTF-8");
			response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("销售报表.xls", "UTF-8"));

			bis = new BufferedInputStream(new FileInputStream(filePath));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
			
			logger.info("[销售报表.xls]导出成功");
		} catch (Exception e) {
			
			e.printStackTrace();
		} finally {
			
			try {
				if (bis != null) {
					bis.close();
				}
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
